package cn.wit.summit.chwan.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import cn.wit.summit.chwan.models.dto.IntentDataBean;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;

/**
 * Created by yandy on 2017/3/7.
 */

public class TextViewWithHyperlink extends android.support.v7.widget.AppCompatTextView {


    public TextViewWithHyperlink(Context context) {
        super(context);
    }

    public TextViewWithHyperlink(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TextViewWithHyperlink(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public int setHyperlink(Activity context, String key, IntentDataBean intent, ClickOther clickOther) {
        if (StringUtils.isEmpty(key)) return -1;
        SpannableString ss = new SpannableString(getText());
        int start = ss.toString().lastIndexOf(key);

        if (start == -1) {
            return -1;
        }

//        while (start != -1) {   //存在此字符串
        int end = start + key.length();
        if (start > 0)
            ss.setSpan(new MyClickableSpan2(context, clickOther), 0, start - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new MyClickableSpan(context, intent), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            if (ss.length()>end){
//            ss.setSpan(new MyClickableSpan2(context, clickOther), end+1, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//
//            }
//            start = ss.toString().indexOf(key, end);
//        }

        setText(ss);
        setMovementMethod(LinkMovementMethod.getInstance());
        return 0;
    }

    public class MyClickableSpan extends ClickableSpan {
        Activity context;
        IntentDataBean intent;

        public MyClickableSpan(Activity context, IntentDataBean intent) {
            this.intent = intent;
            this.context = context;
        }

        @Override
        public void onClick(View widget) {
            IntentUtil.getInstance().intentTo(context, intent);
            widget.callOnClick();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(0xff2a72cb);
        }
    }

    public interface ClickOther {
        void onClickOther();
    }

    public class MyClickableSpan2 extends ClickableSpan {
        Context context;
        ClickOther clickOther;

        public MyClickableSpan2(Context context, ClickOther clickOther) {
            this.clickOther = clickOther;
            this.context = context;
        }

        @Override
        public void onClick(View widget) {
            clickOther.onClickOther();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(0xff2c2c2c);
        }
    }


}
